import pymysql
from pyecharts.charts import Bar
from pyecharts import options

# 建立数据库连接
db = pymysql.connect(
    host="localhost",
    user="fzh",
    password="root",
    database="csw.427.",
    charset="utf8"
)

# 创建游标对象
cursor = db.cursor()

# 修改SQL查询语句，获取每个城市的最低薪水
sql = "select min(job_sal), job_place from job group by job_place order by min(job_sal) limit 5"

# 定义存储数据的列表
sal = []
city = []

try:
    # 执行SQL查询
    cursor.execute(sql)
    res = cursor.fetchall()
    for i in res:
        sal.append(i[0])
        city.append(i[1])
except:
    print("查询出问题了")

# 关闭游标和数据库连接
cursor.close()
db.close()

# 创建柱状图对象
bar = Bar()

# 添加x轴的数据
bar.add_xaxis(city)

# 添加y轴的数据"月薪水/元" 是y轴数据的单位
bar.add_yaxis("月薪水/元", sal)

# 添加图表的标题
bar.set_global_opts(title_opts=options.TitleOpts(title="最低工资的五个城市", subtitle="单位/元"))

# 渲染图表到HTML文件
bar.render("barmin.html")
